package com.hellpro.lolbox.moment.view;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.hellpro.lolbox.R;
import com.hellpro.lolbox.base.BaseFragment;
import com.hellpro.lolbox.config.UrlConfigs;
import com.hellpro.lolbox.factory.MomentFragmentFactory;
import com.hellpro.lolbox.utils.UIManger;
import com.hellpro.lolbox.video.view.VideoSearchActivity;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.utils.Log;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * A simple {@link Fragment} subclass.
 */
public class MomentFragment extends BaseFragment {

    @BindView(R.id.moment_miss)
    RadioButton momentMiss;
    @BindView(R.id.moment_feng)
    RadioButton momentFeng;
    @BindView(R.id.moment_xiaodian)
    RadioButton momentXiaodian;
    @BindView(R.id.moment_cang)
    RadioButton momentCang;
    @BindView(R.id.moment_zhi)
    RadioButton momentZhi;
    @BindView(R.id.moment_jy)
    RadioButton momentJy;
    @BindView(R.id.moment_other)
    RadioButton momentOther;
    @BindView(R.id.moment_fl_content)
    FrameLayout momentFlContent;
    @BindView(R.id.moment_rdg)
    RadioGroup mRg;
    @BindView(R.id.video_iv_search)
    ImageView videoIvSearch;
    @BindView(R.id.video_iv_downLoad)
    ImageView videoIvDownLoad;
    private List<Fragment> fragments;
    private RadioButton[] buttons;
    private int currentIndex = 0;


    @Override
    public void initView() {

        //加载默认的Fragment
        getActivity().getSupportFragmentManager().
                beginTransaction()
                .add(R.id.moment_fl_content, MomentFragmentFactory.createFragment(0))
                .commit();
        initData();
    }

    private void initData() {
//        创建Fragment并传值
        fragments = new ArrayList<>();
        buttons = new RadioButton[mRg.getChildCount()];
        int[] intArray = getResources().getIntArray(R.array.moment_path_id);
        for (int i = 0; i < mRg.getChildCount(); i++) {
            buttons[i] = (RadioButton) mRg.getChildAt(i);
            Fragment fragment = MomentFragmentFactory.createFragment(i);
            Bundle args = new Bundle();
            String path = UrlConfigs.MOMENT_CONTENT_PATH;
            String format = String.format(path, intArray[i]);
            args.putString("url", format);
            fragment.setArguments(args);
            fragments.add(fragment);
        }
        mRg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                for (int i = 0; i < mRg.getChildCount(); i++) {
                    if (buttons[i].getId() == checkedId) {
                        switchFragment(i);
                    }
                }
            }
        });
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_moment;
    }

    /**
     * 每个fragment的之间的交换
     *
     * @param
     */
    private void switchFragment(int id) {
        //目标fragment
        Fragment fragment = fragments.get(id);
        //当前fragment
        Fragment fragment1 = fragments.get(currentIndex);
        //开启一个事务
        FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
        if (fragment.isAdded()) {
            transaction.hide(fragment1).show(fragment);
        } else {
            transaction.hide(fragment1).add(R.id.moment_fl_content, fragment);
        }
        transaction.commit();
        currentIndex = id;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @OnClick({R.id.video_iv_search, R.id.video_iv_downLoad})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.video_iv_search:
                UIManger.startToActivity(getActivity(),VideoSearchActivity.class);
                break;
            case R.id.video_iv_downLoad:

                new ShareAction(getActivity()).withTargetUrl("getActivity()").withTitle("LOL盒子")
                        .setDisplayList(SHARE_MEDIA.SINA,SHARE_MEDIA.QQ,SHARE_MEDIA.WEIXIN)
                        .setCallback(umShareListener).open();
                break;
        }
    }
    private UMShareListener umShareListener = new UMShareListener() {
        @Override
        public void onResult(SHARE_MEDIA platform) {

            Toast.makeText(getActivity(), platform + " 分享成功啦", Toast.LENGTH_SHORT).show();

        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            Toast.makeText(getActivity(),platform + " 分享失败啦", Toast.LENGTH_SHORT).show();
            if(t!=null){
                Log.d("throw","throw:"+t.getMessage());
            }
        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {
            Toast.makeText(getActivity(),platform + " 分享取消了", Toast.LENGTH_SHORT).show();
        }
    };
}
